/**
 * Copyright 2011 Joe Choi, All rights reserved.
 */
package tec.data.persistence.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;

import tao.data.persistence.Persistence;
import tao.data.persistence.Session;
import tec.data.persistence.Configuration;
import tec.data.persistence.PersistenceException;

/**
 * @author Joe Choi
 */
public class JDBCConnector implements Persistence {
	public JDBCConnector(Configuration conf) {
		setConfiguration(conf);
	}
	
	public void setConfiguration(Configuration conf) {
		if(conf == null) throw new IllegalArgumentException("Configuration may not be null!");
		this.configuration = conf;
	}

	@Override
	public Session createSession() {
		try {
			Class.forName(configuration.getDriverName());
			Connection conn = DriverManager.getConnection(
				configuration.getURL(), 
				configuration.getUsername(), 
				configuration.getPassword()
			);
			return(new JDBCSession(conn));
		} catch (Exception e) {
			throw new PersistenceException(e);
		}
	}
	
	private Configuration configuration;
}
